﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management.Automation;
using System.Text;
using MDCAdminTool;

namespace E4D.AppFabric.Entities
{
    public static class MdcCacheEx
    {
        public static Collection<NamedCacheStats> BuildNamedCacheStats( this Collection<PSObject> r )
        {
            var result = new Collection<NamedCacheStats>();

            if ( r != null )
                foreach ( PSObject o in r )
                {
                    result.Add( ConvertPSObjectToNamedCacheStats( o ) );
                }
            return result;
        }
        public static NamedCacheStats ConvertPSObjectToNamedCacheStats( this PSObject o )
        {
            return
                new NamedCacheStats
                {
                    ItemCount    = (long)o.Properties["ItemCount"].Value ,
                    MissCount    = (long)o.Properties["MissCount"].Value ,
                    RegionCount  = (long)o.Properties["RegionCount"].Value ,
                    RequestCount = (long)o.Properties["RequestCount"].Value ,
                    Size         = (long)o.Properties["Size"].Value
                };
        }
        public static CacheConfig ConvertPSObjectToCacheConfig( this PSObject o )
        {
            return
                new CacheConfig
                {
                    CacheName     = (string)o.Properties["CacheName"].Value ,
                    EvictionType  = (string)o.Properties["EvictionType"].Value ,
                    IsExpirable   = (bool)o.Properties["IsExpirable"].Value ,
                    NotificationsEnabled  = (bool)o.Properties["NotificationsEnabled"].Value ,
                    Secondaries    = (int)o.Properties["Secondaries"].Value ,
                    TTL            = (long)o.Properties["TimeToLive"].Value ,
                    CacheType      = (string)o.Properties["CacheType"].Value
                };
        }
    }
}
